#!/bin/bash
img_type_name=$(head -n 1 log1-img_type_name)
img_hardware_name=$(head -n 1 log2-img_hardware_name)
# 配置软件源
if [ $img_type_name == "ubuntukylin" ]
	then
	echo "deb http://ports.ubuntu.com/ubuntu-ports focal main universe multiverse restricted" > /etc/apt/sources.list
	echo "deb http://ports.ubuntu.com/ubuntu-ports focal-updates main universe multiverse restricted" >> /etc/apt/sources.list
	echo "deb http://ports.ubuntu.com/ubuntu-ports focal-backports main universe multiverse restricted" >> /etc/apt/sources.list
	echo "deb http://ports.ubuntu.com/ubuntu-ports focal-security main universe multiverse restricted" >> /etc/apt/sources.list
	touch /etc/apt/sources.list.d/test.list
	rm /etc/apt/sources.list.d/*
elif [ $img_type_name == "openKylin" ]
	then
	echo "deb http://archive.build.openkylin.top/openkylin/ yangtze main cross pty" > /etc/apt/sources.list
	echo "deb http://archive.build.openkylin.top/openkylin/ yangtze-security main cross pty" >> /etc/apt/sources.list
	echo "deb http://archive.build.openkylin.top/openkylin/ yangtze-updates main cross pty" >> /etc/apt/sources.list
	echo "deb http://archive.build.openkylin.top/openkylin/ yangtze-proposed main cross pty" >> /etc/apt/sources.list
	touch /etc/apt/sources.list.d/test.list
	rm /etc/apt/sources.list.d/*
else
	echo "系统类型错误，请检查log1-img_type_name文件中内容,并重新执行该脚本"
	exit
fi
apt-get update
# 配置网卡设置
touch /etc/network/interfaces
echo " auto lo iface lo inet loopback" > /etc/network/interfaces
echo " auto eth0 iface eth0 inet dhcp" >> /etc/network/interfaces
# 配置root密码
echo "请输入要设置的root密码，建议$img_type_name"
passwd
# 设置主机名
echo $img_type_name > /etc/hostname
echo "# <file system> <mount point>   <type>  <options>       <dump>  <pass>" > /etc/fstab 
echo "LABEL=rootfs /               ext4    errors=remount-ro 0       1" >> /etc/fstab 
echo "LABEL=boot  /boot           vfat    nodev,noexec,ro   0       2" >> /etc/fstab
# 配置内核软链接
touch /etc/kernel-img.conf
echo "do_symlinks = No" > /etc/kernel-img.conf
echo "no_symlinks = Yes" >> /etc/kernel-img.conf
# 安装内核
if [ $img_hardware_name == "hifive-unmatched" ]
	then
	apt-get install linux-image-5.11.0-1017-generic u-boot-menu u-boot-sifive initramfs-tools linux-modules-5.11.0-1017-generic linux-modules-extra-5.11.0-1017-generic linux-headers-5.11.0-1017-generic linux-firmware linux-base
	apt-get clean 
	echo mmc_spi >>/etc/initramfs-tools/modules  
	rm /boot/initrd* 
	update-initramfs -c -k all 
	cp  /lib/firmware/5.11.0-1017-generic/device-tree/sifive/hifive-unmatched-a00.dtb /boot/ 
	echo U_BOOT_FDT=\"hifive-unmatched-a00.dtb\" >> /etc/default/u-boot 
	echo U_BOOT_PARAMETERS=\"rw rootwait console=ttySIF0,115200 earlycon\" >> /etc/default/u-boot 
	u-boot-update
	echo "脚本执行结束后请在/boot/extlinux/extlinux.conf中的两个空白行都添加 fdt /hifive-unmatched-a00.dtb"
elif [ $img_hardware_name == "visionfive1" ]
	then
	apt install linux-firmware linux-base
	echo "请在执行完脚本之后从赛昉官网下载visionfive1的内核deb包，并拷贝进入镜像dpkg -i安装，并执行以下命令"
	echo "echo mmc_spi >>/etc/initramfs-tools/modules"
	echo "rm /boot/initrd*"
	echo "update-initramfs -c -k all"
	echo "cp  /lib/firmware/5.11.0-1017-generic/device-tree/sifive/hifive-unmatched-a00.dtb /boot/  #不同版本内核的dtb文件目录可能不同"
	echo "visionfive1安装内核部分命令结束"
else
	echo "适用的硬件类型错误，请检查log2-img_hardware_name文件中内容,并重新执行该脚本"
	exit
fi
# 设置镜像时间
apt-get install openssh-server openntpd ntpdate 
apt-get clean 
sed -i 's/^DAEMON_OPTS="/DAEMON_OPTS="-s /' /etc/default/openntpd 
apt-get install language-pack-zh-hans  language-pack-gnome-zh-hans
# 设置镜像默认语言
echo "这里配置系统默认语言为中文，请稍后选择 zh_CN.UTF-8"
locale-gen zh_CN.UTF-8
echo "选择 zh_CN.UTF-8"
dpkg-reconfigure locales
echo "LANG="zh_CN.UTF-8"" > /etc/default/locale
echo "LANGUAGE="zh_CN:zh"" >> /etc/default/locale
echo "LC_ALL="zh_CN.UTF-8"" >> /etc/default/locale
apt-get install language-selector-common language-selector-gnome
# 镜像中添加用户
echo "请输入要设置的密码，推荐$img_type_name"
adduser $img_type_name
usermod -G sudo -a $img_type_name
# 安装显示相关组件
apt install udisks2 xorg lightdm 
# 安装ukui桌面组件
apt install ukui-desktop-environment ukui-desktop-environment-core ubuntukylin-theme
apt -f install
echo "请手动解决可能出现的apt安装错误，之后执行exit退出chroot，并执行第三段脚本autoBuildImgStep3.sh"



